package com.ruiysoft.conf;

import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import com.ruiysoft.pub.RedisCacheMybatis;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * redis配置类
 *
 * @author zcc ON 2018/3/19
 **/
@Configuration
@EnableCaching//开启缓存注解
public class RedisConfig {
    /**
     * CacheManager  spring-cache提供注解形式缓存，详细可百度，需验证是否好用与限制（使用很方便，可以减少大量缓存相关代码）
     * @param redisConnectionFactory
     * @return
     */
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
        //
        //使用FastJsonRedisSerializer来序列化和反序列化redis的value值（默认使用JDK的序列化方式）
        //此序列化生成json会绑定类
        //如{\"@type\":\"com.ruiysoft.entity.sys.SysUser\",\"id\":11}
        //只能用RedisTemplate<String,com.ruiysoft.entity.sys.SysUser> 接收限制包名类名
        //修改类的属性或serialVersionUID，可正常反序列化，只是无法获取对应属性
        GenericFastJsonRedisSerializer serializer = new GenericFastJsonRedisSerializer();

        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(serializer));
//                .entryTtl(Duration.ofHours(1)); // 设置缓存有效期一小时
        return RedisCacheManager
                .builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
                .cacheDefaults(redisCacheConfiguration).build();
    }
    // 以下两种redisTemplate自由根据场景选择
    @Bean
    public  RedisTemplate<String, ?> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<String, ?> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);

        //使用FastJsonRedisSerializer来序列化和反序列化redis的value值（默认使用JDK的序列化方式）
        //此序列化生成json会绑定类
        //如{\"@type\":\"com.ruiysoft.entity.sys.SysUser\",\"id\":11}
        //只能用RedisTemplate<String,com.ruiysoft.entity.sys.SysUser> 接收限制包名类名
        //修改类的属性或serialVersionUID，可正常反序列化，只是无法获取对应属性
        GenericFastJsonRedisSerializer serializer = new GenericFastJsonRedisSerializer();
//        fastjson

        template.setValueSerializer(serializer);
        //使用StringRedisSerializer来序列化和反序列化redis的key值
        template.setKeySerializer(new StringRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }
    @Bean
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
        stringRedisTemplate.setConnectionFactory(factory);
        return stringRedisTemplate;
    }

//    @Bean
//    public  RedisCacheMybatis redisCacheMybatis(){
//        return new RedisCacheMybatis("a");
//    }

}